package com.lzl.edu.controller.user;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzl.edu.pojo.User;
import com.lzl.edu.service.impl.UserServiceImpl;
import com.lzl.edu.utils.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Mod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <h3>leezhongloong_ws</h3>
 * <p>userController层</p>
 *
 * @author : 黎钟龙
 * QQ:1661548820
 * Mail：1661548820@qq.com
 * @date : 2022-03-18 09:21
 **/
@Controller
@Slf4j
@RequestMapping("/user")
public class UserController {
//    注入USI
    @Resource
    private UserServiceImpl userService;

    /**
     * 跳到主页
     * @return
     */
    @RequestMapping("/index")
    public String index(){
//        "redirect:/queryAll"不加user路径;
        return "redirect:queryAll";
    }

    @GetMapping("/user/{id}")
    public User queryById(@PathVariable Long id){
        User user = userService.queryById(id);
        return user;
    }

    /**
     * 请求到修改页面
     * @param model
     * @return
     */
    @RequestMapping("toupdate")
    public String toUpdate(Model model){
        log.info("跳转到用户信息修改页");
        User user = userService.queryById(1L);
        model.addAttribute("user",user);
        return "user/update";
    }

    /**
     * 更改的方法
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/update")
    public String update(User user,Model model,Integer curPage){
        log.info("调用修改业务");
        System.out.println("update中的curPage"+curPage);
//        将user属性中的""改为null
        User tmpUser= StringUtil.uniteUser(user);
        System.out.println(tmpUser);
        userService.updateById(user);
////        获取修改后的实体
//        User userTemp = userService.queryById(user.getId());
////        向model中添加查询后的实体
//        model.addAttribute("user",userTemp);
//        返回视图
        return "redirect:pageInfo?curPage="+curPage;
    }

    /**
     * 去到insert插入页面
     * @return
     */
    @RequestMapping("/toinsert/{curPage}")
    public String goInsert(@PathVariable Integer curPage,Model model){
        model.addAttribute("curPage",curPage);
        log.debug("去到insert插入页面");
        return "user/insert";
    }

    /**
     *新增一条user对象
     * @param user
     * @return
     */
    @RequestMapping("/insert")
    public String doInsert(User user,Integer curPage){
        log.debug("开始添加对象");
        userService.saveUser(user);
        return "redirect:pageInfo?curPage="+curPage;
    }

    @RequestMapping("/queryAll")
    public String queryAll(Model model){
//        List<User> users = userService.queryAll();
//        model.addAttribute("users",users);
        return "redirect:pageInfo?curPage=1";
    }

    @RequestMapping("/queryById")
    public String queryById(Model model,Long id){
        User user = userService.queryById(id);
        model.addAttribute("user",user);
        return "user/detail";
    }

    /**
     * 根据id更改User
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toupdate/{id}/{curPage}")
    public String toUpdate(@PathVariable Long id,@PathVariable Integer curPage, Model model){
        System.out.println("id--"+id+"--page"+curPage);
        User user = userService.queryById(id);
        model.addAttribute("user",user);
        model.addAttribute("curPage",curPage);
        return "user/update";
    }

    /**
     * 对User删除的方法
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/deleteById")
    public String delete(Long id,Model model,Integer curPage){
        System.out.println("id:---"+id);
        userService.deleteById(id);
        return "redirect:/user/pageInfo?curPage="+curPage;
    }

//  设置每页的大小
    @Value("${page.size}")
    private Integer pageSize;
    @RequestMapping("/pageInfo")
    public String queryByPage(@RequestParam(defaultValue = "1",name = "curPage") String curPage,Model model){
        Integer integer = Integer.valueOf(curPage);
        System.out.println("前端的当前页--"+integer);
        if (integer<1)
            integer=1;
        PageInfo<User> pageInfo = userService.pageInfo(integer, Integer.valueOf(pageSize));
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("success","true");
        return "user/showall";

    }

    @RequestMapping
    /**
     * 注册器编辑器将前端的Date（String）传入后端后统一类型为Date
     * @param binder
     */
    @InitBinder
    public void iniBinder(ServletRequestDataBinder binder){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(format,true));
    }
}
